package modelDeLivreur;

public class Direction {
	
	public Direction(String htmlDirection, String duration, String distance){
		this.htmlDirection = htmlDirection;
		this.distance = distance;
		this.duration = duration;
		this.numDuration = getFirstIntValue(this.duration);
		this.numDistance = getFirstIntValue(this.distance);
	}
	
	// accessor and mutator methods
	public String getHtmlDirection() {
		return htmlDirection;
	}
	public void setHtmlDirection(String htmlDirection) {
		this.htmlDirection = htmlDirection;
	}
	public String getDuration() {
		return duration;
	}
	public void setDuration(String duration) {
		this.duration = duration;
	}
	public String getDistance() {
		return distance;
	}
	public void setDistance(String distance) {
		this.distance = distance;
	}
	public double getNumberDuration() {
		return numDuration;
	}
	public double getNumberDistance() {
		return numDistance;
	}
	
	/**
	 * Method allowing to recover the numerical value of the input String
	 * 
	 * @param input String to be parsed
	 * @return double
	 */
	private double getFirstIntValue(String input){
		double value = 0;
		
		if(input != null && !input.trim().equals("")){
		
		
		try{
			String[] strArray = input.trim().split(" ");
			value = Double.parseDouble(strArray[0]);
		}
		catch(NumberFormatException nExc){
			nExc.printStackTrace();
			value = 0;
		}
		catch(Exception exc){
			exc.printStackTrace();
			value = 0;
		}
		}
		return value;
	}
	
	// for testing purposes
	public String toString(){
		return htmlDirection+"\n"+duration+"----"+distance;
	}
	
	// direction in the form of text with some html tags
	private String htmlDirection = null;
	
	// duration of each travel step
	private String duration = null;
	
	// distance of each travel step
	private String distance = null;
	
	// numerical value of duration
	private double numDuration = 0;
	
	// numerical value of distance
	private double numDistance = 0;
}
